package lovemusic.lanou.com.newsmusic.ui.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import lovemusic.lanou.com.newsmusic.R;
import lovemusic.lanou.com.newsmusic.ui.fragment.Fragment_center;
import lovemusic.lanou.com.newsmusic.ui.fragment.Fragment_music;
import lovemusic.lanou.com.newsmusic.ui.fragment.Fragment_news;
import lovemusic.lanou.com.newsmusic.ui.fragment.Fragment_video;
import lovemusic.lanou.com.newsmusic.utils.UIUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener {
    private ImageView header_img;
    private TextView toolbar_title;
    private ImageView toolbar_right_img;
    private Toolbar toolbar;
    private LinearLayout fl_content;
    private NavigationView navigation_view;
    private DrawerLayout draw_layout;

    private LinearLayout ll_music_tille;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initView() {
        header_img = (ImageView) findViewById(R.id.header_img);
        toolbar_title = (TextView) findViewById(R.id.toolbar_title);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        fl_content = (LinearLayout) findViewById(R.id.fl_content);
        navigation_view = (NavigationView) findViewById(R.id.navigation_view);
        draw_layout = (DrawerLayout) findViewById(R.id.draw_layout);


        ll_music_tille = (LinearLayout) findViewById(R.id.ll_music_tille);

    }

    private void initEvent() {
        setLister();
    }


    public void setLister() {
        header_img.setOnClickListener(this);
        navigation_view.setNavigationItemSelectedListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_img:
                draw_layout.openDrawer(GravityCompat.START);
                break;
        }
    }
    /**
     * 切换Fragemnt
     * @param item
     * @return
     */
    @Override
    public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
        draw_layout.closeDrawers();
        UIUtils.getHandle().postDelayed(new Runnable() {
            @Override
            public void run() {
                item.setChecked(true);
            }
        }, 500);
        switch (item.getItemId()) {
            case R.id.action_news:
                Toast.makeText(this, "action_news", Toast.LENGTH_SHORT).show();
                getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, new Fragment_news()).commit();
                toolbar_title.setVisibility(View.VISIBLE);
                ll_music_tille.setVisibility(View.GONE);
                toolbar_title.setText("新闻中心");
                break;
            case R.id.action_video:
                Toast.makeText(this, "action_video", Toast.LENGTH_SHORT).show();
                getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, new Fragment_video()).commit();
                toolbar_title.setVisibility(View.VISIBLE);
                ll_music_tille.setVisibility(View.GONE);
                toolbar_title.setText("视频中心");
                break;
            case R.id.action_music:
                Toast.makeText(this, "action_music", Toast.LENGTH_SHORT).show();
                getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, new Fragment_music()).commit();
                toolbar_title.setVisibility(View.GONE);
                ll_music_tille.setVisibility(View.VISIBLE);

                break;
            case R.id.action_center:
                getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, new Fragment_center()).commit();
                toolbar_title.setVisibility(View.VISIBLE);
                ll_music_tille.setVisibility(View.GONE);
                toolbar_title.setText("个人中心");
                break;
            case R.id.action_model:
                break;
        }
        return false;

    }


}
